package com.crm.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.crm.dao.CustomerDao;
import com.crm.dao.impl.CustomerDaoImpl;
import com.crm.pojo.Customer;
import com.crm.pojo.PageInfo;
import com.crm.service.CustomerService;

public class CustomerServiceImpl implements CustomerService {
	CustomerDao customerDao = new CustomerDaoImpl();
	@Override
	public List queryDict(String id) {
		List<Map> lists = customerDao.queryDict(id);
		List<String> list = new ArrayList<String>();
		for(Map l:lists) {
			list.add(l.get("dict_item_name").toString());
		}
		return list;
	}
	@Override
	public boolean insertCustomer(Customer customer) {
		boolean flag = false;
		int cusromerIf = customerDao.insertCustomer(customer);
		if(cusromerIf==1) {
			List<Map> custs = customerDao.queryCustomer(customer.getCustName());
			int custId=0;
			for(Map cust:custs) {
				custId = Integer.parseInt(cust.get("cust_id").toString());
			}
			if(custId!=0) {
				int cusromerDetailIf = customerDao.insertCustomerDetail(custId,customer);
				if(cusromerDetailIf==1) {
					flag = true;
				}
			}
		}
		return flag;
	}
	@Override
	public PageInfo queryCustomer(String custName, Integer pageSize, Integer pageNumber) {
		return customerDao.queryCustomer(custName,pageSize,pageNumber);
	}
	@Override
	public boolean deleteCustomer(Integer custId) {
		boolean flag = false;
		int n = customerDao.deleteCustomer(custId);
		if(n==1) {
			flag = true;
		}
		return flag;
	}
	@Override
	public Map quertCustomerByCustId(Integer custId) {
		List<Map> customers = customerDao.quertCustomerByCustId(custId);
		Map customer = new HashMap();
		for(Map c:customers) {
			customer = c;
		}
		return customer;
	}
	@Override
	public boolean updateCustomer(Customer customer) {
		boolean flag = false;
		int cusromerIf = customerDao.updateCustomer(customer);
		if(cusromerIf==1) {
			int cusromerDetailIf = customerDao.updateCustomerDetail(customer);
			if(cusromerDetailIf==1) {
				flag = true;
			
			}
		}
		return flag;
	}
	

}
